﻿using System.Globalization;
using HzyScanDiService.Attributes;
using HzyScanDiService.Extensions;
using HzyScanDiService.Models;

namespace HzyScanDiServiceTest.Services.Impl;

[AopDiService]
public class UserServiceImpl
{
    private readonly ILogger<UserServiceImpl> _logger;
    public UserServiceImpl(ILogger<UserServiceImpl> logger)
    {
        _logger = logger;
    }

    public virtual string GetName(int i)
    {
        var a = 0;
        var res = 5 / a;
        _logger.LogInformation("GetName 函数被执行中....");
        return "Ok" + i;
    }
}

/// <summary>
/// 定义 Aop 拦截标记 和 拦截逻辑
/// </summary>
public class AopDiServiceAttribute : AopBaseAttribute
{

    public AopDiServiceAttribute()
    {
        // this.ExceptionEvent = Exception;
    }

    public override void After(ClassContext classContext, object result, string methodName)
    {
        using var scope = ServiceProviderExtensions.CreateScope();
        var logger = scope.ServiceProvider.GetService<ILogger<AopDiServiceAttribute>>();

        logger?.LogInformation("拦截函数 之后 返回值：" + result + "函数：" + methodName);
    }

    public override void Before(ClassContext classContext, Dictionary<string, object> parameter, string methodName)
    {
        using var scope = ServiceProviderExtensions.CreateScope();
        var logger = scope.ServiceProvider.GetService<ILogger<AopDiServiceAttribute>>();

        logger?.LogInformation("拦截函数 之前 函数：" + methodName);
    }

    // private void Exception(ClassContext classContext, Exception ex, string methodName)
    // {
    //     //函数异常触发事件

    // }


}
